<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <!-- 搜索板块 -->
      <a-row class="row-item-search">
        <a-form class="search-form" :form="searchForm" layout="inline" @submit="handleSearch">
          <a-form-item label="书籍名称">
            <a-input v-decorator="['bookName']" placeholder="请输入书籍名称" />
          </a-form-item>
          <a-form-item label="时间">
            <a-range-picker format="YYYY-MM-DD" v-decorator="['betweenTime', { initialValue: defaultRange }]" />
          </a-form-item>
          <a-form-item class="search-btn">
            <a-button type="primary" icon="search" html-type="submit">搜索</a-button>
          </a-form-item>
        </a-form>
      </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
    </s-table>
    <ItemForm ref="Form" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import moment from 'moment';
import * as Api from '@/api/firmWx'
import { STable } from '@/components'
import ItemForm from './modules/ItemForm'
      const now = new Date();
export default {
  name: 'bookstatistics',
  components: {
    STable,
    ItemForm
  },
  data () {
    return {
      // 当前表单元素
      searchForm: this.$form.createForm(this),
      defaultRange: [moment().subtract(7, 'days'), moment()],
      // 查询参数
      queryParam: {
        book_name: '',
        betweenTime: [moment().subtract(7, 'days'), moment()],
      },
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '书籍名称',
          dataIndex: 'book_name'
        },
        {
          title: '访问量',
          dataIndex: 'count'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.visitBookStatistics({ ...param, ...this.queryParam })
          .then(response => response.data.list)
      },
    }
  },
  created () {
  },
  methods: {
    // 确认搜索
    handleSearch (e) {
      e.preventDefault()
      this.searchForm.validateFields((error, values) => {
        if (!error) {
          this.queryParam = { ...this.queryParam, ...values }
          this.handleRefresh(true)
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }
  }
}
</script>

<style lang="less" scoped>
.ant-card-body {
  padding: 22px 29px 25px;
}
</style>
